package cn.bdqn.service.impl;

import cn.bdqn.mapper.CustomerMapper;
import cn.bdqn.pojo.Customer;
import cn.bdqn.service.CustomerService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CustomerServiceImpl implements CustomerService {
    @Resource
    CustomerMapper customerMapper;
    @Override
    public void addCustomer(Customer customer) {
        customerMapper.insert(customer);
    }

    @Override
    public IPage<Customer> getCustomersByPage(String custName, String custNo, String region, String managerName, String level, Integer pageNo, Integer pageSize) {
        //组装分页条件
        Page<Customer> page=new Page<>(pageNo,pageSize);
        //组合查询条件
        QueryWrapper<Customer> qw=new QueryWrapper<>();
        qw.like(custName!=null&&!"".equals(custName), "cust_name",custName);
        qw.eq(custNo!=null&&!"".equals(custNo),"cust_no",custNo);
        qw.eq(region!=null&&!"".equals(region),"cust_region",region);
        qw.like(managerName!=null&&!"".equals(managerName),"cust_manager_name",managerName);
        qw.like(level!=null&&!"".equals(level),"cust_level",level);

        IPage<Customer> customerIPage = customerMapper.selectPage(page, qw);
        return customerIPage;
    }

    @Override
    public Customer getCustomer(String custNo) {
        QueryWrapper<Customer> qw=new QueryWrapper<>();
        qw.eq("cust_no",custNo);
       return customerMapper.selectOne(qw);

    }

    @Override
    public void updateCustomer(Customer customer) {
        QueryWrapper<Customer> qw=new QueryWrapper<>();
        qw.eq("cust_no",customer.getCustNo());
        customerMapper.update(customer,qw);
    }

    @Override
    public int deleteCustomer(String custNo) {
        QueryWrapper<Customer> qw=new QueryWrapper<>();
        qw.eq("cust_no",custNo);
        return customerMapper.delete(qw);
    }

    @Override
    public List<Customer> findAllCustomers() {
        return customerMapper.selectList(null);
    }


}
